﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Base.Cache;
using Bigger.Framework;

namespace CJAtech.BBS.Process
{
    public static class ArticleInfoHelper
    {
        public static ArticleResultInfo CreateArticleResultInfo(this ArticleInfo objArticleInfo)
        {
            ArticleResultInfo objArticleResultInfo = null;
            if (objArticleInfo == null)
            {
                return objArticleResultInfo;
            }
            return objArticleResultInfo = new ArticleResultInfo
            {
                ArticleInfo = objArticleInfo
            };
        }
        public static ArticleResultInfo CreateArticleResultInfo(this ArticleInfo objArticleInfo, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null, string connectionStringKey = "", string dataObjectParam = "")
        {
            ArticleResultInfo objArticleResultInfo = null;
            if (objArticleInfo == null)
            {
                return objArticleResultInfo;
            }
            objArticleResultInfo = new ArticleResultInfo
            {
                ArticleInfo = objArticleInfo
            };
            return objArticleResultInfo.CreateArticleResultInfo(isLoadArticleContent, isLoadArticlePictureCollection, expire, connectionStringKey, dataObjectParam);
        }
        public static List<ArticleResultInfo> CreateArticleResultInfoList(this List<ArticleInfo> objArticleInfoList, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null, string connectionStringKey = "", string dataObjectParam = "")
        {
            List<ArticleResultInfo> objArticleResultInfoList = null;
            if (objArticleInfoList.IsNull())
            {
                return objArticleResultInfoList;
            }
            if (expire == null)
            {
                expire = TimeSpan.FromSeconds(5);
            }
            return objArticleInfoList.Select(c => c.CreateArticleResultInfo()).ToList().CreateArticleResultInfoList(isLoadArticleContent, isLoadArticlePictureCollection, expire, connectionStringKey, dataObjectParam);
        }

        public static ArticleResultInfo CreateArticleResultInfo(this ArticleResultInfo objArticleResultInfo, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null, string connectionStringKey = "", string dataObjectParam = "")
        {
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                return objArticleResultInfo;
            }
            if (expire == null)
            {
                expire = TimeSpan.FromSeconds(5);
            }
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(connectionStringKey, dataObjectParam);
            ArticleInfoData objArticleInfoData = null;
            if (isLoadArticleContent && isLoadArticlePictureCollection)
            {
                List<ArticleInfoData> objList = objBizArticleInfoData.GetListCache("", expire.Value, string.Format("ArticleId=" + objArticleResultInfo.ArticleInfo.ArticleId));
                objArticleInfoData = objList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                }
                objArticleInfoData = objList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticlePictureCollection);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticlePictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                }
                return objArticleResultInfo;
            }
            if (isLoadArticleContent)
            {
                objArticleInfoData = objBizArticleInfoData.GetRecordCache("", expire.Value, "ArticleId=" + objArticleResultInfo.ArticleInfo.ArticleId + " AND InfoDataType=@InfoDataType", new { InfoDataType = ArticleInfoDataType.ArticleContent });
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                }
            }
            if (isLoadArticlePictureCollection)
            {
                objArticleInfoData = objBizArticleInfoData.GetRecordCache("", expire.Value, "ArticleId=" + objArticleResultInfo.ArticleInfo.ArticleId + " AND InfoDataType=@InfoDataType", new { InfoDataType = ArticleInfoDataType.ArticlePictureCollection });
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticlePictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                }
            }
            return objArticleResultInfo;
        }

        public static List<ArticleResultInfo> CreateArticleResultInfoList(this List<ArticleResultInfo> objArticleResultInfoList, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null, string connectionStringKey = "", string dataObjectParam = "")
        {
            if (objArticleResultInfoList.IsNull())
            {
                return objArticleResultInfoList;
            }
            if (expire == null)
            {
                expire = TimeSpan.FromSeconds(5);
            }
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(connectionStringKey, dataObjectParam);
            List<ArticleInfoData> objList = null;
            ArticleInfoData objArticleInfoData = null;
            if (isLoadArticleContent && isLoadArticlePictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", expire.Value, string.Format("ArticleId in({0})", objArticleResultInfoList.Select(c => c.ArticleInfo.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    List<ArticleInfoData> objArticleInfoDataList = null;
                    foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
                    {
                        objArticleInfoDataList = objList.Where(c => c.ArticleId == objArticleResultInfo.ArticleInfo.ArticleId).ToList();
                        if (objArticleInfoDataList.IsNoNull())
                        {
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                            }
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticlePictureCollection);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.ArticlePictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                            }
                        }
                    }
                }
                return objArticleResultInfoList;
            }
            if (isLoadArticleContent)
            {
                objList = objBizArticleInfoData.GetListCache("", expire.Value, string.Format("ArticleId in({0}) AND InfoDataType=@InfoDataType", objArticleResultInfoList.Select(c => c.ArticleInfo.ArticleId).JoinComma()), new { InfoDataType = ArticleInfoDataType.ArticleContent });
                if (objList.IsNoNull())
                {
                    foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
                    {
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleResultInfo.ArticleInfo.ArticleId);
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                        }
                    }
                }
            }
            if (isLoadArticlePictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", expire.Value, string.Format("ArticleId in({0}) AND InfoDataType=@InfoDataType", objArticleResultInfoList.Select(c => c.ArticleInfo.ArticleId).JoinComma()), new { InfoDataType = ArticleInfoDataType.ArticlePictureCollection });
                if (objList.IsNoNull())
                {
                    foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
                    {
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleResultInfo.ArticleInfo.ArticleId);
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.ArticlePictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                        }
                    }
                }
            }
            return objArticleResultInfoList;
        }
    }
}
